<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/6/19
 * Time: 9:59
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Setsail extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('setsail_flag')
                ->where('sf_status', 1)
                ->count();// 获取总条数
            $lists = Db::name('setsail_flag')
                ->where('sf_status', 1)
                ->field('sf_id, sf_name, sf_image')
                ->page($Nowpage,$limits)
                ->order('sf_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加起航旗
     * @return mixed|\think\response\Json
     */
    public function setsailAdd(){
        if(Request::instance()->isPost()){
            $sf_name = trim(input('sf_name'), ' ');
            $sf_image = input('sf_image');
            $insert = [
                'sf_name' => $sf_name,
                'sf_image' => $sf_image,
                'sf_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_setsail_flag')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('setsail/setsailadd');
    }
    
    /**
     * 删除起航旗
     * @return \think\response\Json
     */
    public function setsailDel(){
        $sf_id = input('id');
        $res = Db::table('up_setsail_flag')->where('sf_id', $sf_id)->setField('sf_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}